package com.lvchangqi.dao;

import com.lvchangqi.entity.InfoEntity;
import com.lvchangqi.entity.LeaveEntity;
import com.lvchangqi.entity.UserEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by version on 17-6-24.
 */
@Service
public class DAO {

    @Autowired
    private SessionFactory factory;

    private Session getSession() {
        return factory.getCurrentSession();
    }


    public InfoEntity getTeacherInfo(String clazz){
        Session session = this.getSession();
        String sql = "SELECT i.* FROM info AS i, user as u " +
                "WHERE i.clazz = :clazz AND u.info_id = i.id AND u.role = 1";
        Query query = session.createNativeQuery(sql, InfoEntity.class)
                .setParameter("clazz", clazz);

        return (InfoEntity) query.list().get(0);
    }

    public UserEntity getUser(String id){
        Session session = this.getSession();
        int index = 0;
        if(id.startsWith("0")){
            index = 1;
            id = id.substring(1);
        }
        Query query = session.createQuery("from UserEntity where student_id = :id")
                .setParameter("id", id);
        return query.list().size() == 0 ? null : (UserEntity) query.list().get(index);
    }

    public Integer saveCase(LeaveEntity leaveEntity){
        Session session = this.getSession();
        return (Integer) session.save(leaveEntity);
    }

    public void updateLeave(Map<String, String> map){
        Session session = this.getSession();
        LeaveEntity leaveEntity = session.get(LeaveEntity.class, Integer.valueOf(map.get("id")));

        if(map.containsKey("t_version")){
            Integer t_version = Integer.valueOf(map.get("t_version"));
            leaveEntity.setT_version(t_version);
            if(t_version == 1){
                leaveEntity.setL_version(0);
            }
        } else {
            leaveEntity.setL_version(Integer.valueOf(map.get("l_version")));
        }

        leaveEntity.setReject(map.get("reject_reason"));

        session.saveOrUpdate(leaveEntity);
    }

    public List getLeaves(Integer infoId, Integer role){
        Session session = this.getSession();
        Query query = null;

        if(role == 0){
            String sql = "select l.* from leaves l, user u where " +
                    "u.info_id = :infoId and l.s_id = u.id";
            query = session.createNativeQuery(sql, LeaveEntity.class)
                    .setParameter("infoId", infoId);
        }else if(role == 1){
            InfoEntity info = session.get(InfoEntity.class, infoId);
            String hql = "from LeaveEntity where clazz = :clazz";
            query = session.createQuery(hql)
                    .setParameter("clazz", info.getClazz());
        } else if(role == 2){
            String hql = "from LeaveEntity where l_version is not null";
            query = session.createQuery(hql);
        }

        return query.list();
    }

    public LeaveEntity getLeave(Integer id){
        Session session = this.getSession();

        return session.get(LeaveEntity.class, id);
    }
}
